added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CSASPNETReverseAJAX / ClientAdapter.cs
blobac1793b52a6d8bf9085db08596f501aa039fabe4
1 /****************************** Module Header ******************************\
2 * Module Name: ClientAdapter.cs
3 * Project: CSASPNETReverseAJAX
4 * Copyright (c) Microsoft Corporation
6 * ClientAdapter class manages multiple client instances. The presentation layer
7 * calls its methods to easily send and receive messages.
8 *
9 * This source is subject to the Microsoft Public License.
10 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
11 * All other rights reserved.
13 \*****************************************************************************/
15 using System.Collections.Generic;
17 namespace CSASPNETReverseAJAX
19 /// <summary>
20 /// This class is used to send events/messages to multiple clients.
21 /// </summary>
22 public class ClientAdapter
24 /// <summary>
25 /// The recipient list.
26 /// </summary>
27 private Dictionary<string, Client> recipients = new Dictionary<string,Client>();
29 /// <summary>
30 /// Send a message to a particular recipient.
31 /// </summary>
32 public void SendMessage(Message message)
34 if (recipients.ContainsKey(message.RecipientName))
36 Client client = recipients[message.RecipientName];
38 client.EnqueueMessage(message);
42 /// <summary>
43 /// Called by a individual recipient to wait and receive a message.
44 /// </summary>
45 /// <returns>The message content</returns>
46 public string GetMessage(string userName)
48 string messageContent = string.Empty;
50 if (recipients.ContainsKey(userName))
52 Client client = recipients[userName];
54 messageContent = client.DequeueMessage().MessageContent;
57 return messageContent;
60 /// <summary>
61 /// Join a user to the recipient list.
62 /// </summary>
63 public void Join(string userName)
65 recipients[userName] = new Client();
68 /// <summary>
69 /// Singleton pattern.
70 /// This pattern will ensure there is only one instance of this class in the system.
71 /// </summary>
72 public static ClientAdapter Instance = new ClientAdapter();
73 private ClientAdapter() { }